/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import request from '@/utils/request';
import isArray from 'lodash/isArray';
import { message } from 'antd';
import type { Options } from '@/components/_pages/jdk';

export async function getBondList(params: any): Promise<any> {
  return request('/Inn/getBondLogList', {
    method: 'POST',
    data: params,
  }).then((res) => {
    const { list = [], total } = res.data;
    if (isArray(list) && list.length > 0) {
      return { data: list, total };
    }
    if (res && typeof res.data === 'string') {
      window.location.href = res.data;
    } else if (res.code !== 0) {
      message.error(res.msg);
    }
    return {};
  });
}

export async function getChildrenArea(params: any, isLeaf = false): Promise<Options[]> {
  return request('/Inn/getChildrenArea', {
    method: 'POST',
    data: params,
  }).then(async ({ data }) => {
    if (data && isArray(data)) {
      return data.map((item) => ({
        label: item.name,
        value: item.id,
        code: item.code,
        isLeaf,
      }));
    }
    return [];
  });
}

export async function approveBondBackApply(params: any): Promise<any> {
  return request('/Inn/approveBondBackApply', {
    method: 'POST',
    data: params,
  }).then((res) => {
    return res;
  });
}

export async function bondAreaDelete(params: any): Promise<any> {
  return request('/Inn/bondAreaDelete', {
    method: 'POST',
    data: params,
  }).then((res) => {
    return res;
  });
}

export async function getBondAreaList() {
  return request('/Inn/getBondAreaList', {
    method: 'POST',
  });
}

export async function addRegionAdmin(params: any) {
  return request('/Inn/bondAreaEdit', {
    method: 'POST',
    data: params,
  });
}
